namespace HabraReader.Classes
{
    using System;
    using System.Collections.Specialized;
    using System.Text;
    using System.Xml;

    public static class Options
    {
        private static XmlDocument OptionsXmlReader;

        private static XmlTextWriter OptionsXmlWriter;

        public static StringDictionary ReadOptions()
        {
            string optionsfile = Environment.CurrentDirectory + @"\options.xml";
            var options = new StringDictionary();
            OptionsXmlReader = new XmlDocument();

            OptionsXmlReader.Load(optionsfile);
            XmlNodeList curentOption;

            curentOption = OptionsXmlReader.GetElementsByTagName("Database");
            options.Add("Database", curentOption.Item(0).InnerText);

            curentOption = OptionsXmlReader.GetElementsByTagName("UpdateFolder");
            options.Add("UpdateFolder", curentOption.Item(0).InnerText);

            curentOption = OptionsXmlReader.GetElementsByTagName("WindowState");
            options.Add("WindowState", curentOption.Item(0).InnerText);

            curentOption = OptionsXmlReader.GetElementsByTagName("WindowWidth");
            options.Add("WindowWidth", curentOption.Item(0).InnerText);

            curentOption = OptionsXmlReader.GetElementsByTagName("WindowHeight");
            options.Add("WindowHeight", curentOption.Item(0).InnerText);

            curentOption = OptionsXmlReader.GetElementsByTagName("WindowPosition.X");
            options.Add("WindowPosition.X", curentOption.Item(0).InnerText);

            curentOption = OptionsXmlReader.GetElementsByTagName("WindowPosition.Y");
            options.Add("WindowPosition.Y", curentOption.Item(0).InnerText);

            curentOption = OptionsXmlReader.GetElementsByTagName("SplitterDistance");
            options.Add("SplitterDistance", curentOption.Item(0).InnerText);

            return options;
        }

        public static void WriteDefaultOptions()
        {
            try
            {
                OptionsXmlWriter = new XmlTextWriter(Environment.CurrentDirectory + @"\options.xml", Encoding.Unicode);
                OptionsXmlWriter.Formatting = Formatting.Indented;
                OptionsXmlWriter.WriteStartDocument();
                OptionsXmlWriter.WriteStartElement("HabraReader");
                OptionsXmlWriter.WriteElementString("Database", "data.db3");
                OptionsXmlWriter.WriteElementString("UpdateFolder", "updates");
                OptionsXmlWriter.WriteElementString("WindowState", "Normal");
                OptionsXmlWriter.WriteElementString("WindowWidth", "800");
                OptionsXmlWriter.WriteElementString("WindowHeight", "500");
                OptionsXmlWriter.WriteElementString("WindowPosition.X", "50");
                OptionsXmlWriter.WriteElementString("WindowPosition.Y", "50");
                OptionsXmlWriter.WriteElementString("SplitterDistance", "100");
                OptionsXmlWriter.WriteEndElement();
                OptionsXmlWriter.WriteEndDocument();
                OptionsXmlWriter.Close();
            }
            catch (Exception e)
            {
            }
        }

        public static void WriteOptions(StringDictionary options)
        {
            try
            {
                OptionsXmlWriter = new XmlTextWriter(Environment.CurrentDirectory + @"\options.xml", Encoding.Unicode);
                OptionsXmlWriter.Formatting = Formatting.Indented;
                OptionsXmlWriter.WriteStartDocument();
                OptionsXmlWriter.WriteStartElement("HabraReader");
                OptionsXmlWriter.WriteElementString("Database", options["Database"]);
                OptionsXmlWriter.WriteElementString("UpdateFolder", options["UpdateFolder"]);
                OptionsXmlWriter.WriteElementString("WindowState", options["WindowState"]);
                OptionsXmlWriter.WriteElementString("WindowWidth", options["WindowWidth"]);
                OptionsXmlWriter.WriteElementString("WindowHeight", options["WindowHeight"]);
                OptionsXmlWriter.WriteElementString("WindowPosition.X", options["WindowPosition.X"]);
                OptionsXmlWriter.WriteElementString("WindowPosition.Y", options["WindowPosition.Y"]);
                OptionsXmlWriter.WriteElementString("SplitterDistance", options["SplitterDistance"]);
                OptionsXmlWriter.WriteEndElement();
                OptionsXmlWriter.WriteEndDocument();
                OptionsXmlWriter.Close();
            }
            catch (Exception e)
            {
            }
        }
    }
}